package com.ruoyi.dgw.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.mapper.BankManagerMapper;
import com.ruoyi.dgw.domain.BankManager;
import com.ruoyi.dgw.service.IBankManagerService;

/**
 * 银行管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-23
 */
@Service
public class BankManagerServiceImpl implements IBankManagerService 
{
    @Autowired
    private BankManagerMapper bankManagerMapper;

    /**
     * 查询银行管理
     * 
     * @param id 银行管理主键
     * @return 银行管理
     */
    @Override
    public BankManager selectBankManagerById(Long id)
    {
        return bankManagerMapper.selectBankManagerById(id);
    }

    /**
     * 查询银行管理列表
     * 
     * @param bankManager 银行管理
     * @return 银行管理
     */
    @Override
    public List<BankManager> selectBankManagerList(BankManager bankManager)
    {
        return bankManagerMapper.selectBankManagerList(bankManager);
    }

    /**
     * 新增银行管理
     * 
     * @param bankManager 银行管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBankManager(BankManager bankManager)
    {
        bankManager.setCreateTime(DateUtils.getNowDate());
        int rows = bankManagerMapper.insertBankManager(bankManager);
        insertTagNumber(bankManager);
        return rows;
    }

    /**
     * 修改银行管理
     * 
     * @param bankManager 银行管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBankManager(BankManager bankManager)
    {
        bankManager.setUpdateTime(DateUtils.getNowDate());
        bankManagerMapper.deleteTagNumberByLoans(bankManager.getId());
        insertTagNumber(bankManager);
        return bankManagerMapper.updateBankManager(bankManager);
    }

    /**
     * 批量删除银行管理
     * 
     * @param ids 需要删除的银行管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBankManagerByIds(Long[] ids)
    {
        bankManagerMapper.deleteTagNumberByLoanss(ids);
        return bankManagerMapper.deleteBankManagerByIds(ids);
    }

    /**
     * 删除银行管理信息
     * 
     * @param id 银行管理主键
     * @return 结果
     */
    @Override
    public int deleteBankManagerById(Long id)
    {
        bankManagerMapper.deleteTagNumberByLoans(id);
        return bankManagerMapper.deleteBankManagerById(id);
    }

    /**
     * 新增耳标信息
     * 
     * @param bankManager 银行管理对象
     */
    public void insertTagNumber(BankManager bankManager)
    {
        List<TagNumber> tagNumberList = bankManager.getTagNumberList();
        Long id = bankManager.getId();
        if (StringUtils.isNotNull(tagNumberList))
        {
            List<TagNumber> list = new ArrayList<TagNumber>();
            for (TagNumber tagNumber : tagNumberList)
            {
                tagNumber.setLoans(id);
                list.add(tagNumber);
            }
            if (list.size() > 0)
            {
                bankManagerMapper.batchTagNumber(list);
            }
        }
    }
}
